using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Xml.Serialization;

namespace dender.dvdcover.core
{
    /// <summary>
    /// Defines an image container of a cover. Images of a cover can only be loaded
    /// in parts
    /// </summary>
    public class Part : DimensionBase
    {

        public event EventHandler<PartImageChangedEventArgs> OnPartImageChanged;

        private PointF mLocation;
        /// <summary>
        /// Gets or Sets the upper left corner of the part, this point must be
        /// in the area of the cover
        /// </summary>
        /// <exception cref="System.ArgumentOutOfRangeException">
        /// Throws an ArgumentOutOfRangeException when the location is 
        /// outside the cover
        /// </exception>
        public PointF Location
        {
            get
            {
                return mLocation;
            }
            set
            {
                mLocation = value;
            }
        }

        private string mName;
        /// <summary>
        /// Gets or Sets the name of the part
        /// </summary>
        public string Name
        {
            get
            {
                return mName;
            }
            set
            {
                mName = value;
            }
        }

        private Format mFormat;
        /// <summary>
        /// Gets the format of the wich the part belong to.
        /// </summary>
        [XmlIgnore()] 
        public Format Format
        {
            get
            {
                return mFormat;
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("Format");
                mFormat = value;
            }
        }
        
        private CoverImage  mImage;
        /// <summary>
        /// Gets the image of the part.
        /// </summary>
        [XmlIgnore()] 
        public CoverImage  Image
        {
            get { return mImage; }
        }

        /// <summary>
        /// Internal constructor for XmlSerialize
        /// </summary>
        internal Part()
        {
        }

        public Part(Format format)
        {
            this.Format = format;
        }
        public Part(Format format, PointF location , float width, float height) : base(width,height)
        {
            this.Format = format;
            this.Location = location;
        }

        /// <summary>
        /// Fills the part with a specified image
        /// </summary>
        /// <param name="image">Image used to fill the part</param>
        public void Fill(CoverImage image)
        {
            this.mImage = image;
            this.mImage.OnImageChanged += new EventHandler<ImageChangedEventArgs>(OnImageChanged);
            EventTrigger<PartImageChangedEventArgs>.Invoke(OnPartImageChanged, this, new PartImageChangedEventArgs(this.mImage));
        }

        /// <summary>
        /// Occurs when the ImageChangeEvent of the underlining CoverImage is triggered
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnImageChanged(object sender, ImageChangedEventArgs e)
        {
            EventTrigger<PartImageChangedEventArgs>.Invoke(OnPartImageChanged,this,new PartImageChangedEventArgs(this.mImage));
        }

        /// <summary>
        /// Clears the part, by removing the image
        /// </summary>
        public void Clear()
        {
            this.mImage = null;
            EventTrigger<PartImageChangedEventArgs>.Invoke(OnPartImageChanged, this, new PartImageChangedEventArgs(this.mImage));
        }
    }
}
